package com.android.stockme.frontend;

import java.util.ArrayList;

import com.android.stockme.backend.IBackendController;
import com.android.stockme.backend.portfolio.IStockData;
import com.android.stockme.backend.portfolio.IStockPortfolioUpdatedListener;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class PortfolioAdapter extends BaseAdapter implements ListAdapter {

	private IBackendController backend = null;
	private Context context = null;
	private StockMePortfolioTab portfolioTab = null;

	private LayoutInflater inflater = null;

	private ArrayList<IStockData> stocks = null;
	
	public PortfolioAdapter(final IBackendController backend, Context context, StockMePortfolioTab portfolioTab) {
		this.backend = backend;
		this.context = context;
		this.portfolioTab = portfolioTab;
		
		this.stocks = this.backend.getStockPortfolio().getStocks();
		
		this.backend.getStockPortfolio().registerStockPortfolioUpdatedListener(new IStockPortfolioUpdatedListener() {
			public void notifyStockPortfolioUpdated(Object arg) {
				stocks = backend.getStockPortfolio().getStocks();
				notifyDataSetChanged();
			}			
		});
	}
	
	public int getCount() {
		return this.stocks.size();
	}

	public Object getItem(int position) {
		return this.stocks.get(position);
	}

	public long getItemId(int position) {
		return position;
	}

	public int getItem(String symbol) {
		for (int i = 0; i < this.stocks.size(); i++)
			if (symbol.equals(this.stocks.get(i).getSymbol()))
				return i;
		return -1;
	}
	
	public View getView(int position, View convertView, ViewGroup parent) {
		inflater = LayoutInflater.from(context);
		RelativeLayout cellLayout = (RelativeLayout)inflater.inflate(R.layout.stock_node, null);
		cellLayout.setMinimumWidth(parent.getWidth());
		int color;
		
		IStockData stock = this.stocks.get(position);

		if (stock.isExists() == false) {
			color = Color.rgb(0,0,0);
		}
		else {
			if(position % 2 > 0)
				color = Color.rgb(48,92,131);
			else 
				color = Color.rgb(119,138,170);			
		}
		cellLayout.setBackgroundColor(color);
		
		TextView field = (TextView)cellLayout.findViewById(R.id.symbol);
		field.setBackgroundColor(color);
		field.setText(stock.getSymbol());
		field.setClickable(true);
		field.setOnClickListener(this.portfolioTab);

		field = (TextView)cellLayout.findViewById(R.id.current);
		field.setText(Float.toString(stock.getValue()));
		field.setClickable(true);
		field.setOnClickListener(this.portfolioTab);

		field = (TextView)cellLayout.findViewById(R.id.change);
		field.setClickable(true);
		field.setOnClickListener(this.portfolioTab);
		
		String change = ((stock.getRelativeChange() < 0) ? Float.toString(stock.getRelativeChange()) : ("+" + Float.toString(stock.getRelativeChange())));

		if (stock.isExists() == false) {
			field.setBackgroundColor(0x00000000);			
		}
		else {
			if(stock.getRelativeChange() >= 0)
				field.setBackgroundColor(0xff2e8b57);
			else
				field.setBackgroundColor(0xffb22222);			
		}
		
		field.setText(change);

		cellLayout.setId(position);
		cellLayout.setClickable(true);
		cellLayout.setOnClickListener(this.portfolioTab);
		return cellLayout;
	}
}
